package com.ttg.utils.connection.handler;

//~--- non-JDK imports --------------------------------------------------------

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ResponseHandler;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.IOException;

//~--- JDK imports ------------------------------------------------------------

//~--- classes ----------------------------------------------------------------

/**
 * Class FileResponseHandler
 * Description
 * Create 2016-02-25 01:04:20 
 * @author Ardy    
 */
public class FileResponseHandler implements ResponseHandler<File> {

    /** 
     * Field downloadFile
     * Description 
     */
    private final File downloadFile;

    /**
     * Constructs FileResponseHandler
     * Description
     *
     * @param downloadFile 类型：File
     */
    public FileResponseHandler(File downloadFile) {
        if (( downloadFile != null ) && ( !downloadFile.exists() ||!downloadFile.isDirectory() )) {
            this.downloadFile = downloadFile;
        } else {
            throw new IllegalArgumentException("The downloadFile is required; it must not be null or directory.");
        }
    }

    /**
     * Method handleResponse 
     * Description 说明：
     *
     * @param response 说明：
     *
     * @return 返回值说明：
     *
     * @throws IOException 异常：
     */
    public File handleResponse(HttpResponse response) throws IOException {
        HttpEntity entity = null;
        File       var4;

        try {
            entity = ResponseHandlerHelper.handleNon2xxStatus(response);

            File targetFile = ResponseHandlerHelper.copy(entity.getContent(), this.downloadFile);

            var4 = targetFile;
        } finally {
            EntityUtils.consume(entity);
        }

        return var4;
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
